import { pinia } from "@/pinia"
import { resetRouter } from "@/router"
import { routerConfig } from "@/router/config"
import { getUserInfoApi } from "@@/apis/user"
import { setToken as _setToken, getToken, removeToken } from "@@/utils/cache/cookies"
import { useSettingsStore } from "./settings"
import { useTagsViewStore } from "./tags-view"




export const useUserStore = defineStore("user", () => {
  const token = ref<string>(getToken() || "")
  const roles = ref<string[]>([])
  const username = ref<string>("")
  const username_ru = ref<string>("")
  const mobile = ref<string>("");
  const level = ref<number>(0)
  const bid =  ref(localStorage.getItem('bid'))
  const IM_SDKAppID = ref<number>(1400818966);
  const IM_secretKey = ref<string>("fbb62384b4544dbb81878e6af400affada719aecb9ddebe6c1221868f959635c");
  const PERFIX =  ref<string>("gluz_");
  const firstLogin = ref(localStorage.getItem('firstLogin'))
  const openIM = ref<boolean>(false);
  const openIMAI = ref<boolean>(false);
  const userSig = ref<string>("")


  // 头像
  const avatar = ref<string>("")


  const tagsViewStore = useTagsViewStore()
  const settingsStore = useSettingsStore()


  // 设置 Token
  const setToken = async (value: string) => {
    _setToken(value)
    token.value = value;
  }

  // 设置 openIM
  const setOpenIM = async (value: boolean) => {
    openIM.value = value;
  }

  // 设置 openIMAI
  const setOpenIMAI = async (value: boolean) => {
    openIMAI.value = value;
  }

  // 设置 openIMAI
  const setUserSig = async (value: string) => {
    userSig.value = value;
  }

  // 设置bid
  const setBid = (val: any)=> {
    bid.value = val;
    localStorage.setItem('bid',val)
  }

  // 设置firstLogin
  const setFirstLogin = (val: any)=> {
    firstLogin.value = val;
    localStorage.setItem('firstLogin',val)
  }

  // 获取用户详情
  const getInfo = async () => {
    const { data } = await getUserInfoApi()
    // console.log("data=======", data)
    username.value = data.username;
    username_ru.value = data.username_ru;
    mobile.value = data.mobile;
    avatar.value = data.avatar;
    level.value = data.level;
    let isFirst = !data.address && data.bus_status==0  ? "true" : "false";
    // let isFirst =  data.bus_status==0  ? "true" : "false";
    setFirstLogin(isFirst);

    // 验证返回的 roles 是否为一个非空数组，否则塞入一个没有任何作用的默认角色，防止路由守卫逻辑进入无限循环
    roles.value = ['admin'];
  }

  // 模拟角色变化
  const changeRoles = async (role: string) => {
    const newToken = `token-${role}`
    token.value = newToken
    _setToken(newToken)
    // 用刷新页面代替重新登录
    location.reload()
  }

  // 登出
  const logout = () => {
    removeToken()
    token.value = ""
    roles.value = []
    username.value = "";
    mobile.value = "";
    avatar.value = "";
    level.value = 0;
    userSig.value = "";
    resetRouter()
    resetTagsView()
  }

  // 重置 Token
  const resetToken = () => {
    removeToken()
    token.value = ""
    roles.value = []
  }

  // 重置 Visited Views 和 Cached Views
  const resetTagsView = () => {
    if (!settingsStore.cacheTagsView) {
      tagsViewStore.delAllVisitedViews()
      tagsViewStore.delAllCachedViews()
    }
  }

  return { token, roles, username,username_ru,mobile,avatar,level,bid,IM_SDKAppID,IM_secretKey,PERFIX,firstLogin,openIM,openIMAI,userSig,setBid, setToken, setOpenIM, setOpenIMAI, setUserSig, setFirstLogin, getInfo, changeRoles, logout, resetToken }
})

/**
 * @description 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
 * @description 在 SSR 应用中可用于在 setup 外使用 store
 */
export function useUserStoreOutside() {
  return useUserStore(pinia)
}
